package dateStructure;

import java.util.Scanner;

public class listQueue {
    private static final int Maxsize = 107;

    private static class Node {
        int date;
        Node next;
    }

    private static class Link {
        Node front;
        Node rear;
    }

    private static boolean initQueue(Link Q) {
        Q.front = Q.rear=new Node();
        Q.front.next=Q.rear.next=null;
        return true;
    }

    private static boolean inQueue(Link Q, int e) {
        Node p = new Node();
        p.date = e;
        p.next=null;
        Q.rear.next=p;
        Q.rear = p;
        return true;
    }

    private static int outQueue(Link Q) {
        Node p;//建立一个新的结点存要出队的结点！
        if(Q.front==Q.rear){
            System.out.println("队列已空!");
            System.exit(0);
        }
        p = Q.front.next;
        Q.front.next = p.next;
        if(Q.front.next==p)///头结点的下一个就是要删除的结点时，说明只有一个元素，这时候直接头尾指针相等就行
            Q.rear=Q.front;
        return p.date;
    }

    public static int getElem(Link Q) {
        return Q.front.next.date;
    }
    public static void main(String arg[]) {
        Link MyQ = new Link();
        int x = -1;
        System.out.println("该队列总容量为：" + Maxsize);
        if(initQueue(MyQ)){
         System.out.println("初始化成功!");
        }
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入元素的个数：");
        int n = scanner.nextInt();
        int sum = n;
        System.out.print("请依次输入各个元素：");
        while (n-- > 0) {
            x = scanner.nextInt();
            inQueue(MyQ, x);
        }
        System.out.println("队头元素为：" + getElem(MyQ));
//        System.out.println("队列的长度为：" + getLength(MyQ));
        System.out.println("出队元素为：");
        while (sum-- > 0) {
            System.out.print(outQueue(MyQ) + " ");
        }
        System.out.println();
    }
}
